// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stores: [
      {
        storeId: 1,
        storeName: '店铺A',
        selected: false,
        products: [
          {
            productId: 1,
            logo: 'https://cdn-we-retail.ym.tencent.com/tsr/goods/dz-3a.png?imageMogr2/thumbnail/585x585/quality/70/strip/format/webp',
            name: '商品1',
            spec: '规格1',
            price: 100,
            quantity: 1,
            stock: 10,
            selected: false
          },
          {
            productId: 2,
            logo: 'https://cdn-we-retail.ym.tencent.com/tsr/goods/dz-3a.png?imageMogr2/thumbnail/585x585/quality/70/strip/format/webp',
            name: '商品2',
            spec: '规格2',
            price: 200,
            quantity: 1,
            stock: 5,
            selected: false
          }
        ]
      },
      {
        storeId: 2,
        storeName: '店铺B',
        selected: false,
        products: [
          {
            productId: 3,
            logo: 'https://cdn-we-retail.ym.tencent.com/tsr/goods/dz-3a.png?imageMogr2/thumbnail/585x585/quality/70/strip/format/webp',
            name: '商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3',
            spec: '规格3',
            price: 150,
            quantity: 1,
            stock: 8,
            selected: false
          }
        ]
      },
      {
        storeId: 3,
        storeName: '店铺C',
        selected: false,
        products: [
          {
            productId: 4,
            logo: 'https://cdn-we-retail.ym.tencent.com/tsr/goods/dz-3a.png?imageMogr2/thumbnail/585x585/quality/70/strip/format/webp',
            name: '商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3商品3',
            spec: '规格3',
            price: 150,
            quantity: 1,
            stock: 8,
            selected: false
          }
        ]
      }
    ],
    allSelected: false,
    totalPrice: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 匹配当前页面索引，触发tabbar底部聚焦项变更
    this.getTabBar().init();
  },

  toggleStoreSelect(e) {
    const storeId = e.target.dataset.storeId;
    const stores = this.data.stores.map(store => {
      if (store.storeId === storeId) {
        store.selected = !store.selected;
        store.products.forEach(product => product.selected = store.selected);
      }
      return store;
    });
    this.setData({ stores }, this.updateTotalPrice);
  },

  toggleProductSelect(e) {
    const productId = e.target.dataset.productId;
    const stores = this.data.stores.map(store => {
      store.products = store.products.map(product => {
        if (product.productId === productId) {
          product.selected = !product.selected;
        }
        return product;
      });
      store.selected = store.products.every(product => product.selected);
      return store;
    });
    this.setData({ stores }, this.updateTotalPrice);
  },

  toggleAllSelect(e) {
    const allSelected = !this.data.allSelected;
    const stores = this.data.stores.map(store => {
      store.selected = allSelected;
      store.products.forEach(product => product.selected = allSelected);
      return store;
    });
    this.setData({ stores, allSelected }, this.updateTotalPrice);
  },

  inputQuantity(e){
    console.log(e);
    var inputVale = e.detail.value;
    const { index, storeIndex } = e.currentTarget.dataset;
    const stores = this.data.stores;
    const product = stores[storeIndex].products[index];
    if (inputVale > product.stock) {
      inputVale = product.stock;
      wx.showToast({
        title: '购买数量不可大于剩余库存！',
        icon: 'none'
      })
    }
    if (inputVale < 1) {
      inputVale = 1;
      wx.showToast({
        title: '购买数量不可小于1！',
        icon: 'none'
      })
    }
    console.log('值修改');
    product.quantity = inputVale;
    this.setData({ stores }, this.updateTotalPrice);
  },

  increaseQuantity(e) {
    const { index, storeIndex } = e.currentTarget.dataset;
    const stores = this.data.stores;
    const product = stores[storeIndex].products[index];
    if (product.quantity < product.stock) {
      product.quantity++;
    }else{
      return;
    }
    this.setData({ stores }, this.updateTotalPrice);
  },

  decreaseQuantity(e) {
    const { index, storeIndex } = e.currentTarget.dataset;
    const stores = this.data.stores;
    const product = stores[storeIndex].products[index];
    if (product.quantity > 1) {
      product.quantity--;
    }else{
      return;
    }
    this.setData({ stores }, this.updateTotalPrice);
  },

  updateTotalPrice() {
    let totalPrice = 0;
    var allSelected = true;
    this.data.stores.forEach(store => {
      store.products.forEach(product => {
        if (product.selected) {
          totalPrice += product.price * product.quantity;
        }else{
          allSelected = false;
        }
      });
    });
    
    this.setData({ totalPrice, allSelected });
  },

  goToCheckout() {
    // 跳转到结算页面
    wx.navigateTo({
      url: '/pages/checkout/checkout'
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})